package invoicesystem.dao.impl;

import invoicesystem.dao.EmailDao;
import invoicesystem.entry.Email;
import invoicesystem.util.DBHelper;

import java.util.ArrayList;

public class EmailDaoImpl implements EmailDao {
    private DBHelper db=new DBHelper();
    @Override
    public int insertEmail(Email email) {
        String sql=" insert into email values(null,?,?,?) ";
        return db.updateinfo(sql,email.getEnterprise_id(),email.getEmail_detail(),email.getDefault_status());
    }

    @Override
    public int updateEmail(Email email) {
        String sql ="update email set email_detail=? where id=? ";
        return db.updateinfo(sql,email.getEmail_detail(),email.getId());
    }

    @Override
    public ArrayList<Email> select(Integer enterprise_id) {
        String sql=" select * from email where enterprise_id=? ";
        return db.selectinfo(sql,Email.class,enterprise_id);
    }

    @Override
    public ArrayList<Email> selectAll() {
        String sql=" select * from email ";
        return db.selectinfo(sql,Email.class);
    }


        @Override
        public int updateStatus(Email email) {
            String sql ="update email set default_status=?  where id=? ";
            return db.updateinfo(sql,email.getDefault_status(),email.getId());
        }

    @Override
    public Email selectByEmail_detail(String email_detail) {
        String sql="select * from email where enterprise_id=?";
        ArrayList<Email> list=db.selectinfo(sql,Email.class,email_detail);
        return list.get(0);
    }

    @Override
    public ArrayList<Email> selectById(int enterprise_id) {
        String sql="select * from email where enterprise_id=?";
        ArrayList<Email> list=db.selectinfo(sql,Email.class,enterprise_id);
        return list;
    }

    @Override
    public ArrayList<Email> selectAll(int id) {
        String sql="select * from email";
        return db.selectinfo(sql,Email.class);
    }

    @Override
    public Email selectDefault(Integer enterprise_id) {
        ArrayList<Email> list=selectById(enterprise_id);
        ArrayList<Email> emails=new ArrayList<>();
        for (Email e:list){
            if (e.getDefault_status().equals("默认")){
                emails.add(e);
                System.out.println(e);
            }
        }
        return list.get(0);
    }

}
